package com.lgr.store.utils;

import org.springframework.stereotype.Service;

import com.lgr.store.dao.SystemLogDAO;
import com.lgr.store.vo.EmpVo;
import com.lgr.store.vo.SystemlogVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class LogJournal {
    @Resource
    private  HttpSession session;
    @Resource
    private HttpServletRequest request;
    @Resource
    private SystemLogDAO systemLogDAO;
    private  String getIpAddr(HttpServletRequest request) {
        //处理代理访问获取不到真正的ip问题的
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            //获取代理中中的ip
            ip = request.getHeader("PRoxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            //获取代理中中的ip

            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            //非代理的情况获取ip
            ip = request.getRemoteAddr();
        }

        return ip;
    }


    public  void saveUserLog(Map<Integer,Object> map) {
        EmpVo emp = ContextUtils.getUserInfo();
        SystemlogVo ul = null;
        String opr = "";
        Integer keys[]= map.keySet().toArray(new Integer[0]);
        int logtype=keys[0];
        Object obj=map.get(keys[0]);
        switch (logtype) {
            case 1:
                opr = "新增";
                break;
            case 2:
                opr = "修改";
                break;
            case 3:
                opr = "删除";
                break;
            case 4:
                opr = "登录";
                break;
            case 5:
                opr = "作废";
                break;
            case 6:
                opr = "修改密码";
            case 7:
                opr = "app扫码登录";
                break;
            case 8:
                opr = "初始化密码";
                break;
            case 9:
                opr = "更改状态";
                break;
        }

        String tables = obj.getClass().getName();
        int pos = tables.lastIndexOf(46);
        tables = tables.substring(pos + 1) + ":" + opr;
        tables = tables.substring(0, tables.lastIndexOf("Vo")) + ":" + opr;
        ul=new SystemlogVo();
        ul.setEmpId(emp.getEmpId());
        ul.setIpAddr(getIp2(request));
        ul.setTables(tables);
        ul.setOptime(ContextUtils.dateToStrLong(new Date()));
        ul.setMsg(obj.toString());
        systemLogDAO.save(ul);
    }
    private   String getIp2(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if(StringUtil.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if(index != -1){
                return ip.substring(0,index);
            }else{
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if(StringUtil.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            return ip;
        }
        return request.getRemoteAddr();
    }
    //设置日志变量
    public static Map<Integer,Object> setLogParam(int logtype,Object obj){
        Map<Integer,Object> map=new HashMap<Integer,Object>();
        map.put(logtype,obj);
        return map;
    }

    public static void main(String[] args) {
        Map<Integer,Object> map=new HashMap<Integer,Object>();
        map.put(10,"测试");
        Integer keys[]= map.keySet().toArray(new Integer[0]);
        System.out.println(keys[0]);
    }
}
